import pygame
from ui.button import Button

class BuildingMenu:
    def __init__(self, x, y, width, height):
        self.rect = pygame.Rect(x, y, width, height)
        self.buttons = []
        
        # 使用系统字体
        try:
            # Windows系统
            self.title_font = pygame.font.SysFont('Microsoft YaHei', 32)
            self.info_font = pygame.font.SysFont('Microsoft YaHei', 24)
        except:
            try:
                # Linux系统
                self.title_font = pygame.font.SysFont('WenQuanYi Micro Hei', 32)
                self.info_font = pygame.font.SysFont('WenQuanYi Micro Hei', 24)
            except:
                # 如果都失败，使用默认字体
                self.title_font = pygame.font.Font(None, 32)
                self.info_font = pygame.font.Font(None, 24)
        
        # 建筑类型定义
        self.building_types = {
            'command_center': {
                'name': '指挥中心',
                'size': (4, 4),
                'color': (200, 200, 200),
                'cost': 400,
                'description': '主要建筑，提供补给'
            },
            'barracks': {
                'name': '兵营',
                'size': (3, 3),
                'color': (150, 150, 150),
                'cost': 150,
                'description': '训练步兵单位'
            },
            'supply_depot': {
                'name': '补给站',
                'size': (2, 2),
                'color': (100, 100, 100),
                'cost': 100,
                'description': '增加补给上限'
            },
            'engineering_bay': {
                'name': '工程站',
                'size': (3, 2),
                'color': (200, 150, 100),
                'cost': 125,
                'description': '研究步兵升级'
            },
            'refinery': {
                'name': '精炼厂',
                'size': (3, 3),
                'color': (100, 200, 100),
                'cost': 75,
                'description': '采集瓦斯'
            }
        }
        
        self.create_buttons()
    
    def create_buttons(self):
        y = 100
        for building_type, info in self.building_types.items():
            button = Button(
                self.rect.x + 10,
                y,
                self.rect.width - 20,
                50,
                f"{info['name']} ({info['cost']}矿物)",
                (100, 100, 100),
                (150, 150, 150)
            )
            button.building_type = building_type
            self.buttons.append(button)
            y += 60
    
    def draw(self, screen, minerals):
        # 绘制菜单背景
        pygame.draw.rect(screen, (50, 50, 50), self.rect)
        
        # 绘制标题
        title = self.title_font.render("建筑菜单", True, (255, 255, 255))
        screen.blit(title, (self.rect.x + 10, 10))
        
        # 绘制资源信息
        minerals_text = self.info_font.render(f"矿物: {minerals}", True, (255, 255, 255))
        screen.blit(minerals_text, (self.rect.x + 10, 50))
        
        # 绘制按钮和建筑信息
        y = 100
        for button in self.buttons:
            building_type = self.building_types[button.building_type]
            button.color = (100, 100, 100) if minerals >= building_type['cost'] else (50, 50, 50)
            button.draw(screen)
            
            # 绘制建筑详细信息
            size_text = self.info_font.render(f"规格: {building_type['size'][0]}x{building_type['size'][1]}", True, (200, 200, 200))
            desc_text = self.info_font.render(building_type['description'], True, (200, 200, 200))
            
            screen.blit(size_text, (button.rect.x, button.rect.bottom + 5))
            screen.blit(desc_text, (button.rect.x, button.rect.bottom + 25))
            
            y += 60
    
    def handle_event(self, event):
        for button in self.buttons:
            if button.handle_event(event):
                return button.building_type
        return None 